package com.matt.service.impl;

import com.matt.domain.statistics.StatNurseryCount;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.utils.JsonValidateUtil;
import com.matt.foundation.utils.ModelUtil;
import com.matt.repository.statistics.StatNurseryCountRepository;
import com.matt.service.StatNurseryCountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/8/7
 * *******************************
 */
@Service
@Slf4j
public class StatNurseryCountServiceImpl implements StatNurseryCountService {

    @Autowired
    private StatNurseryCountRepository statNurseryCountRepository;

    @Transactional
    @Override
    public Boolean updateNurseryLimitCount(String areaId, Integer nurseryCount, Integer limitCount, Date date, String extendJson) {
        String today = DateUtil.millisToDateTime(date.getTime(), "yyyy-MM-dd");
        Date begin = DateUtil.getCurrentDayBeginAndEnd(today).get("beg");
        Date end = DateUtil.getCurrentDayBeginAndEnd(today).get("end");
        StatNurseryCount statNurseryCount = statNurseryCountRepository.findByAreaIdAndCreatedBetween(areaId, begin, end);
        if (Objects.nonNull(statNurseryCount)){
            statNurseryCount.setLimitCount(limitCount);
            statNurseryCount.setNurseryCount(nurseryCount);
            statNurseryCount.setExtendJson(extendJson);
        }else {
            statNurseryCount = new StatNurseryCount();
            statNurseryCount.setLimitCount(limitCount);
            statNurseryCount.setAreaId(areaId);
            statNurseryCount.setNurseryCount(nurseryCount);
            statNurseryCount.setCreated(date);
            statNurseryCount.setExtendJson(extendJson);
        }

        statNurseryCountRepository.save(statNurseryCount);
        return true;
    }
}
